<template>
    <div class="edit-popup">
        <popup
            ref="popupRef"
            :async="true"
            :title="popupTitle"
            width="550px"
            @close="handleClose"
            @confirm="handleSubmit"
        >
            <el-form ref="formRef" :model="formData" :rules="formRules" label-width="120px">
                <el-form-item label="反馈类型" prop="type">
                    <el-select v-model="formData.type" clearable disabled placeholder="请选择反馈类型">
                        <el-option :value="1" label="设备报修" />
                        <el-option :value="2" label="订单反馈" />
                        <el-option :value="3" label="合作加盟" />
                        <el-option :value="4" label="意见反馈" />
                    </el-select>
                </el-form-item>
                <el-form-item label="订单ID" prop="order_id">
                    <el-input v-model="formData.order_id" clearable disabled placeholder="请输入订单ID" />
                </el-form-item>
                <el-form-item label="设备ID" prop="device_id">
                    <el-input v-model="formData.device_id" clearable disabled placeholder="请输入设备ID" />
                </el-form-item>
                <el-form-item label="内容" prop="content">
                    <el-input v-model="formData.content" clearable disabled placeholder="请输入内容" type="textarea" />
                </el-form-item>
                <el-form-item label="图片" prop="images">
                    <el-image
                        v-for="(item, index) in formData.images.split(',')"
                        :key="index"
                        :preview-src-list="['https://boom.wuwo.wang/'+item]"
                        :src="'https://boom.wuwo.wang/'+item"
                        style="width: 100px; height: 100px; margin-right: 10px" />

                </el-form-item>
                <el-form-item label="反馈状态" prop="status">
                    <el-tag v-if="formData.status == 1"> 已发起</el-tag>
                    <el-tag v-if="formData.status == 2"> 已处理</el-tag>
                </el-form-item>
                <el-form-item label="反馈结果" prop="result">
                    <el-input v-model="formData.result" clearable placeholder="请输入反馈结果" type="textarea" />
                </el-form-item>
            </el-form>
        </popup>
    </div>
</template>

<script lang="ts" name="boomFeedbackEdit" setup>
import type { FormInstance } from 'element-plus'
import Popup from '@/components/popup/index.vue'
import { apiBoomFeedbackDetail, apiBoomFeedbackReply } from '@/api/boom_feedback'
import type { PropType } from 'vue'

defineProps({
    dictData: {
        type: Object as PropType<Record<string, any[]>>,
        default: () => ({})
    }
})
const emit = defineEmits(['success', 'close'])
const formRef = shallowRef<FormInstance>()
const popupRef = shallowRef<InstanceType<typeof Popup>>()
const mode = ref('add')


// 弹窗标题
const popupTitle = computed(() => {
    return mode.value == 'edit' ? '编辑意见反馈表' : '回复'
})

// 表单数据
const formData = reactive({
    id: '',
    user_id: '',
    agent_id: '',
    type: '',
    order_id: '',
    device_id: '',
    content: '',
    images: '',
    result: '',
    status: ''
})


// 获取详情
const setFormData = async (data: Record<any, any>) => {
    for (const key in formData) {
        if (data[key] != null && data[key] != undefined) {
            //@ts-ignore
            formData[key] = data[key]
        }
    }


}

const getDetail = async (row: Record<string, any>) => {
    const data = await apiBoomFeedbackDetail({
        id: row.id
    })
    setFormData(data)
}


// 提交按钮
const handleSubmit = async () => {
    await apiBoomFeedbackReply({ result: formData.result, id: formData.id })
    popupRef.value?.close()
    emit('success')
}

//打开弹窗
const open = (type = 'add') => {
    mode.value = type
    popupRef.value?.open()
}

// 关闭回调
const handleClose = () => {
    emit('close')
}


defineExpose({
    open,
    setFormData,
    getDetail
})
</script>
